package th.retrofit.lib;

public final class ListNode {

    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }

    @Override
    public String toString() {
        return next == null ? String.valueOf(val) : val + " -> " + next.toString();
    }

    public static ListNode arr2Node(int... arr) {
        if (arr.length == 0) return null;
        ListNode head = new ListNode(arr[0]);
        ListNode tail = head;
        for (int i = 1; i < arr.length; i++) {
            tail.next = new ListNode(arr[i]);
            tail = tail.next;
        }
        return head;
    }


    public static void main(String[] args) {
        ListNode head = new ListNode(5, new ListNode(4, new ListNode(3,
                new ListNode(2, new ListNode(1)))));
        System.out.println(head);
    }

}
